<?php

namespace app\store\model\addons;

use app\common\model\addons\Modules as ModulesModel;
use app\common\model\addons\ModulesBindings;
/**
 * 模块模型
 * Class Apply
 * @package app\store\model\dealer
 */
class Modules extends ModulesModel
{
    /**
     * 获取模块列表
     * @param string $search
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($search = '')
    {
        // 构建查询规则
        $this->order(['create_time' => 'desc']);
        // 查询条件
        !empty($search) && $this->where('title|name', 'like', "%$search%");
        // 获取列表数据
        $list =  $this->paginate(15, false, [
            'query' => \request()->request()
        ]);
       // var_dump($list);
        foreach ($list as &$v){
            $v['type'] = array('wxapp');
            unset($v);
        }
        return $list;
    }
    /**
     * 卸载模块
     * @param string $m_id
     * @return unknown
     */
    public function uninstall($m_id=''){
        $model = new ModulesBindings;
        $model->where(array('m_id'=>$m_id))->delete();
        return  $this->where(array('m_id'=>$m_id))->delete();
    }
    public function getMenus($module,$do=''){
        if(empty($module)){
            return false;
        }
        $model = new ModulesBindings;
        $iscall = $model->where(array('module'=>$module,'call'=>array('neq','')))->find();
        $menus = array();
        if(!empty($iscall)){
            require WEB_PATH.'/extend/we/loader.class.php';
            require WEB_PATH.'/extend/we/WeModuleSite.class.php';
            require ADDONS_PATH.$module.'/site.php';
            session('iscall',true);
            $module_class = $module."ModuleSite";
            $module_class = "\\".ucwords($module_class);
            $site = new $module_class();
            $do = $iscall['call'];
            $menus_list = $site->$do();
            foreach ($menus_list as &$v){
                $position = strrpos($v['url'],'/');
                $do = substr($v['url'],$position+1);
                $v['do'] = $do;
                unset($v);
            }
        }else{
            session('iscall',false);
            $menus_list = $model->where(array('module'=>$module,'entry'=>'menu'))->select()->toArray();
        }
        foreach ($menus_list as &$v){
            if($do==$v['do']){
                $active = true;
            }else{
                $active = false;
            }
            $menus[$v['do']] = array(
                'name'=>$v['title'],
                'icon'=>'icon-setting',
                'index'=>'addons/run',
                'do'=>array('module'=>$module,'do'=>$v['do']),
                'active'=>$active
            );
        }

        return  $menus;
    }
    public function getModuleDetail($module =''){
       $data = $this->where(array('identifie'=>$module))->find();
       return $data;
    }
    public function install($data){
        // 开启事务
        ///var_dump($data);die();
        $this->startTrans();
        try {
            // 添加模块
            $application = $data['application'];
            $modules_data = array(
                'name'=>$application['name'],
                'identifie'=>$application['identifie'],
                'version'=>$application['version'],
                'ability'=>$application['ability'],
                'description'=>$application['description'],
                'author'=>$application['author'],
                'url'=>$application['url'],
                'logo'=>$application['logo']

            );
            $this->save($modules_data);
            $mid = $this->m_id;
            $modules_bindings = array();
            $entry_list = array('page','menu','cover');
            foreach ($entry_list as $entry){
                if(!empty($data['bindings'][$entry])){
                    foreach ($data['bindings'][$entry] as $v){
                        $modules_bindings[] =array_merge(array('module'=>$application['identifie'],'m_id'=>$mid,'entry'=>$entry),$v);
                    }
                }

            }


            // 导航
             $this->addModulesBindings($modules_bindings);
            // 预留其他表的操作







            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
    public function addModulesBindings($data){
        $model = new ModulesBindings;
        return $model->saveAll($data);

    }
}